
/**
* 返回第一个匹配,返回格式: {index:matchIndex,match:matchStr}
*/
String.prototype.first=function(pattern, igm) {
	var out = {index:-1, match:null};
	if (!pattern)
		return out;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return out;
			}
		}
			
		pattern = new RegExp(pattern, igm);
	}
	var match;
	if ((match = pattern.exec(this)) != null){
		out.index = match.index;
		out.match = match[0];
	}
	return out;
};

/**
* 返回最后一个匹配,返回格式: {index:matchIndex,match:matchStr}
*/
String.prototype.last=function(pattern, igm) {
	var out = {index:-1, match:null};
	if (!pattern)
		return out;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return out;
			}
		}
		pattern = new RegExp(pattern, igm);
	}
	if (!pattern.global){
		var newRegOp = (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '') + 'g';
		pattern = new RegExp(this.source, newRegOp);
	}
	var match;
	while ((match = pattern.exec(this)) != null){
		out.index = match.index;
		out.match = match[0];
	}
	return out;
};

/**
* 返回最后一个匹配,返回格式: {index:matchIndex,match:matchStr}
*/
String.prototype.all=function(pattern, igm) {
	var out = [];
	if (!pattern)
		return out;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return out;
			}
		}
		pattern = new RegExp(pattern, igm);
	}
	if (!pattern.global){
		var newRegOp = (pattern.ignoreCase ? 'i' : '') + (pattern.multiline ? 'm' : '') + 'g';
		pattern = new RegExp(this.source, newRegOp);
	}
	var match;
	while ((match = pattern.exec(this)) != null)
		out.push({index:match.index,match:match[0]});
	return out;
};

/**
* 删除行首尾的匹配字符串，默认空白字符
*/
String.prototype.trimStartEnd=function(pattern, igm) {
	pattern = pattern || '\s';
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String)
			return this;
		pattern = new RegExp(pattern, igm);
	}
	var out = this;
	var result = out.first(out);
	while (result.index == 0){
		out = out.substring(0, result.match.length);
		result = out.first(out);
	}
		
	result = out.last(out);
	while (result.index + result.match.length == out.length){
		out = out.substring(result.index);
		result = out.last(out);
	}
    return out;
};
/**
* 删除行首的匹配字符串，默认空白字符
*/
String.prototype.trimStart=function(pattern, igm) {
	pattern = pattern || '\s';
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String)
			return this;
		pattern = new RegExp(pattern, igm);
	}
	var out = this;
	var result = out.first(out);
	while (result.index == 0){
		out = out.substring(0, result.match.length);
		result = out.first(out);
	}
    return out;
};
/**
* 删除行尾的匹配字符串，默认空白字符
*/
String.prototype.trimEnd=function(pattern, igm) {
	pattern = pattern || '\s';
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String)
			return this;
		pattern = new RegExp(pattern, igm);
	}
	var out = this;
	var result = out.last(out);
	while (result.index + result.match.length == out.length){
		out = out.substring(result.index);
		result = out.last(out);
	}
    return out;
};
/**
* 返回第一个匹配字符串之前的
*/
String.prototype.subBefore=function(pattern, igm) {
	if (!pattern)
		return null;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return null;
			}
		}
		pattern = new RegExp(pattern, igm);
	}
	var result = this.first(pattern);
	if (result.index == -1)
		return null;
	return this.substring(0, result.index);
};
/**
* 返回第一个匹配字符串之后的
*/
String.prototype.subAfter=function(pattern, igm) {
	if (!pattern)
		return null;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return null;
			}
		}
		pattern = new RegExp(pattern, igm);
	}
	var result = this.first(pattern);
	if (result.index == -1)
		return null;
	return this.substring(result.index + result.match.length);
};
/**
* 返回最后一个匹配字符串之前的
*/
String.prototype.subBeforeLast=function(pattern, igm) {
	if (!pattern)
		return null;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return null;
			}
		}
		pattern = new RegExp(pattern, igm);
	}
	var result = this.last(pattern);
	if (result.index == -1)
		return null;
	return this.substring(0, result.index);
};
/**
* 返回最后一个匹配字符串之后的
*/
String.prototype.subAfterLast=function(pattern, igm) {
	if (!pattern)
		return null;
	if (pattern.constructor !== RegExp){
		igm = igm || '';
		if (pattern.constructor !== String){
			try{
				pattern = JSON.stringify(pattern);
			}catch(ex){
				return null;
			}
		}
		pattern = new RegExp(pattern, igm);
	}
	var result = this.last(pattern);
	if (result.index == -1)
		return null;
	return this.substring(result.index + result.match.length);
};
/**
* 在指定位置插入新的子字符串
*/
String.prototype.insert=function(pos, str) {
	pos = pos || 0;
	str = str || '';
	if (pos < 0)
		pos = 0;
	if (pos > this.length)
		pos = this.length;
	return this.substring(0,pos) + str + this.substring(pos);
};

/**
* 在指定位置删除指定长度的子字符串
*/
String.prototype.delete=function(pos, len) {
	pos = pos || 0;
	len = len || 0;
	str = str || '';
	if (pos < 0)
		pos = 0;
	if (pos > this.length)
		pos = this.length;
	if (len < 0)
		len = 0;
	if (pos + len > this.length)
		len = this.length - pos;
	return this.substring(0,pos) + this.substring(pos + len);
};
/**
* 仿照Array.splice(), 在指定位置删除指定长度的子字符串，并替换为新子字符串
*/
String.prototype.splice=function(pos, len, str) {
	pos = pos || 0;
	len = len || 0;
	str = str || '';
	if (pos < 0)
		pos = 0;
	if (pos > this.length)
		pos = this.length;
	if (len < 0)
		len = 0;
	if (pos + len > this.length)
		len = this.length - pos;
	return this.substring(0,pos) + str + this.substring(pos + len);
};

module.exports = function(s){return new String(s);};